﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using FeedReaderApp.Model;
using FeedReaderApp.ViewModel;
using GalaSoft.MvvmLight;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229

namespace FeedReaderApp
{
    public sealed partial class GroupDetailPage : FeedReaderApp.Common.LayoutAwarePage
    {
        public GroupDetailViewModel ViewModel { get; set; }

        public GroupDetailPage()
        {
            this.InitializeComponent();

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                ViewModel = new GroupDetailViewModel();
                DataContext = ViewModel;
            }
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested;
            ViewModel.RegisterDataSourceEvents();

            string navParam = (string)navigationParameter;

            // Allow saved page state to override the initial item to display
            if (pageState != null && pageState.ContainsKey(Constants.GroupDetailPageState))
            {
                navParam = (string)pageState[Constants.GroupDetailPageState];
            }

            if (String.IsNullOrWhiteSpace(navParam))
            {
                Debug.WriteLine("GroupDetailPage: Navigation Parameter ist leer.");
                return;
            }

            ViewModel.NavigationParameter = navParam;

            if (navParam.StartsWith(Constants.BlogPrefix))
            {
                string blog = navParam.Substring(Constants.BlogPrefix.Length);
                ViewModel.Initialize(LoadByEnum.FeedName, blog);
            }
        }

        protected override void SaveState(Dictionary<string, object> pageState)
        {
            DataTransferManager.GetForCurrentView().DataRequested -= OnDataRequested;
            pageState[Constants.GroupDetailPageState] = ViewModel.NavigationParameter;
            ViewModel.DeRegisterDataSourceEvents();
        }

        private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            try
            {
                ViewModel.Share(e);
            }
            catch
            {
            }
        }

        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var itemId = ((FeedItem)e.ClickedItem).UniqueId;
            this.Frame.Navigate(typeof(ItemDetailPage), itemId);
        }
    }
}
